#include "OpenGLSupport.h"

#include "OpenGLHelper.h"
#include "../../HObject.h"
#include "../../Math/Vector.h"
#include "../RenderHelper.h"
#include "../MeshRenderer.h"
#include "SimpleOpenGLMeshRenderer.h"

#include "OpenGLExt.h"


namespace helix
{

	OpenGLHelper::OpenGLHelper()
	{
		APIType=H_OPEN_GL_API;          //use the openGL graphics library
		CalcNorms=0;					//don't calculate normals every frame
		VSyncEnabled=true;
		GLextensions = new OpenGLExtensions();     //get access to opengl extensions
		GLextensions->Print();

		meshRenderer = new SimpleOpenGLMeshRenderer();

		if(GLextensions->Supported(H_MULTI_TEX_SUPPORT))
			glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB,&maxTextureUnits);
		else
		{
			maxTextureUnits=1;
		}
	}


	OpenGLHelper::~OpenGLHelper()
	{
		delete GLextensions;
		delete meshRenderer;
	}

	bool OpenGLHelper::setVSync(bool VSyncOn)
	{
		if(!GLextensions->Supported(H_SWAP_CONTROL))
		{   
			Log().Error("Cannot set VSync, Extension not supported");
			return false;        //cannot set VSync
		}
		else if (VSyncOn)
		{
			wglSwapIntervalEXT(1);   // turn it on
			VSyncEnabled=true;
		}
		else
		{
			wglSwapIntervalEXT(0);   // turn it off
			VSyncEnabled=false;
		}
		Log().Println("VSync enabled = %d",VSyncEnabled);
		return true;
	}



	//functions related to points
	void OpenGLHelper::setPointSize(float size)                       //update the point size state
	{
		glPointSize(size);
	}


	void OpenGLHelper::drawPoint3f(float vert[3])
	{
		glBegin(GL_POINTS);
			glVertex3fv(vert);
		glEnd();
	}

	void OpenGLHelper::drawMesh(Mesh *mesh)
	{
		meshRenderer->renderMesh(mesh);
	}

}